/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.smart.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.smart.common.Constants;
import com.smart.common.WebResponse;
import com.smart.common.annotation.WebAccess;
import com.smart.common.request.LoginRequest;
import com.smart.common.service.RedisService;
import com.smart.context.SessionContext;
import com.smart.model.vo.JiansheUnitVO;
import com.smart.model.vo.OperateUserVO;
import com.smart.model.vo.ProjectVO;
import com.smart.model.vo.SuperviseUserVO;
import com.smart.service.CommonUserService;
import com.smart.utils.CookieUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhanghong
 * @version LoginController.java, v 0.1 2023/6/19 15:22 下午 LoginController Exp $$
 */
@RestController
@RequestMapping("/user")
@Api(tags = {"A通用模块#登录模块"})
public class LoginController {

    @Autowired
    private CommonUserService commonUserService;
    @Autowired
    private RedisService redisService;

    @ApiOperation(value = "云监管平台登录", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/supervise/login")
    public WebResponse<SuperviseUserVO> superviseLogin(HttpServletRequest request, HttpServletResponse response, @RequestBody LoginRequest loginRequest) {
        SuperviseUserVO userVO = commonUserService.superviseLogin(loginRequest);
        request.getSession().setAttribute(Constants.LOGIN_USER, userVO);
        response.addCookie(new Cookie(Constants.SESSIONID, CookieUtil.getValue(request, Constants.SESSIONID)));
        return WebResponse.build(userVO);
    }
    @WebAccess(login = true)
    @ApiOperation(value = "云监管平台登出", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/supervise/logout")
    public WebResponse superviseLogout(HttpServletRequest request) {
        request.getSession().removeAttribute(Constants.LOGIN_USER);
        return WebResponse.build(true);
    }
    @WebAccess(login = true)
    @ApiOperation(value = "云监管平台登录用户信息", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/supervise/me")
    public WebResponse<SuperviseUserVO> superviseMe() {
        SuperviseUserVO superviseUserVO = commonUserService.superviseMe();
        return WebResponse.build(superviseUserVO);
    }

    @ApiOperation(value = "云服务平台登录", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/service/login")
    public WebResponse<ProjectVO> serviceLogin(HttpServletRequest request, HttpServletResponse response, @RequestBody LoginRequest loginRequest) {
        ProjectVO projectVO = commonUserService.serviceLogin(loginRequest);
        request.getSession().setAttribute(Constants.LOGIN_USER, projectVO);
        response.addCookie(new Cookie(Constants.SESSIONID, CookieUtil.getValue(request, Constants.SESSIONID)));
        return WebResponse.build(projectVO);
    }
    @WebAccess(login = true)
    @ApiOperation(value = "云服务平台登出", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/service/logout")
    public WebResponse serviceLogout(HttpServletRequest request) {
        request.getSession().removeAttribute(Constants.LOGIN_USER);
        return WebResponse.build(true);
    }
    @WebAccess(login = true)
    @ApiOperation(value = "云服务平台登录用户信息", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/service/me")
    public WebResponse<ProjectVO> serviceMe() {
        return WebResponse.build(commonUserService.serviceMe());
    }

    @ApiOperation(value = "云管理平台登录", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/manage/login")
    public WebResponse<JiansheUnitVO> manageLogin(HttpServletRequest request, HttpServletResponse response, @RequestBody LoginRequest loginRequest) {
        JiansheUnitVO jiansheUnitVO = commonUserService.manageLogin(loginRequest);
        request.getSession().setAttribute(Constants.LOGIN_USER, jiansheUnitVO);
        response.addCookie(new Cookie(Constants.SESSIONID, CookieUtil.getValue(request, Constants.SESSIONID)));
        return WebResponse.build(jiansheUnitVO);
    }
    @WebAccess(login = true)
    @ApiOperation(value = "云管理平台登出", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/manage/logout")
    public WebResponse manageLogout(HttpServletRequest request) {
        request.getSession().removeAttribute(Constants.LOGIN_USER);
        return WebResponse.build(true);
    }
    @WebAccess(login = true)
    @ApiOperation(value = "云管理平台登录用户信息", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/manage/me")
    public WebResponse<JiansheUnitVO> manageMe() {
        return new WebResponse(commonUserService.manageMe());
    }

    @ApiOperation(value = "运营管理平台登录", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/operate/login")
    public WebResponse<OperateUserVO> operateLogin(HttpServletRequest request, HttpServletResponse response, @RequestBody LoginRequest loginRequest) {
        OperateUserVO operateUserVO = commonUserService.operateLogin(loginRequest);
        StpUtil.login(operateUserVO.getId(), Constants.TERMINAL);
        request.getSession().setAttribute(Constants.LOGIN_USER, operateUserVO);
        response.addCookie(new Cookie(Constants.SESSIONID, CookieUtil.getValue(request, Constants.SESSIONID)));
        return WebResponse.build(operateUserVO);
    }
    @WebAccess(login = true)
    @ApiOperation(value = "运营管理平台登出", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/operate/logout")
    public WebResponse operateLogout(HttpServletRequest request) {
        StpUtil.logout(SessionContext.getOperateUserId());
        request.getSession().removeAttribute(Constants.LOGIN_USER);
        return WebResponse.build(true);
    }
    @WebAccess(login = true)
    @ApiOperation(value = "运营管理平台登录用户信息", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/operate/me")
    public WebResponse<OperateUserVO> operateMe() {
        return WebResponse.build(commonUserService.operateMe());
    }
}
